package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 加入购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("添加购物车 。。。 shoppingCart：{} ",shoppingCart.toString());
        //设置用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查询当前菜品或套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
            //如果添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //如果添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        //如果已存在则在原来的基础上+1
        if (cartServiceOne!=null){
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            //调用service更新数量
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //如果不存在则再执行插入购物车操作
            //设置数量为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /**
     * 减少数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("减少数量...... shoppingCart :{}",shoppingCart);
        //获取当前户id
        Long userId = BaseContext.getCurrentId();
        //查询当前菜品或套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        if (dishId!=null){
            //如果减少的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //如果减少的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        //如果数量>1则在原来的基础上-1
        if (cartServiceOne.getNumber()>1){
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number-1);
            //调用service更新数量
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //如果=1在则再执行删除购物车操作
            shoppingCartService.remove(queryWrapper);
        }
        return R.success(cartServiceOne);
    }



    /**
     * 查看购物车数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> listCart(){
        log.info("查询购物车数据....");
        //根据当前userId查询数据库
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(currentId!=null,ShoppingCart::getUserId,currentId);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        //调用service层
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }


    @DeleteMapping("/clean")
    public R<String> clean(){
        log.info("清空购物车....");
        //获取当前uid
        Long userId = BaseContext.getCurrentId();
        //根据userId查询购物车数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        //清空购物车数据
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
}
